package web;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Topic{
	public static void main(String[] args) throws SQLException{
		

	}
	
	public static List getTopics(int nowpage) throws ClassNotFoundException, SQLException{
		List topics=new ArrayList();
		Dao dao=new Dao();
		ResultSet rs;
		try {
			rs = dao.show("SELECT topic.tid,users.uname,topic.theme,topic.create_time,MAX(post.pid) FROM topic JOIN users ON topic.uid=users.uid JOIN post ON topic.tid=post.tid GROUP BY post.tid ORDER BY MAX(post.pid) DESC limit "+(nowpage-1)*10+",10;");
			while(rs.next()){
				Map row=new HashMap();
				row.put("uname", rs.getString("uname"));
				row.put("tid", rs.getString("tid"));
				row.put("theme", rs.getString("theme"));
				row.put("create_time", rs.getString("create_time"));
				topics.add(row);
				
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			throw e;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw e;
		}
		dao.close();
		return topics;
	}
	public static List getPosts(int tid,int nowpage) throws ClassNotFoundException, SQLException{
		List posts=new ArrayList();
		Dao dao=new Dao();
		ResultSet rs;
			try {
				rs = dao.show("select users.uname,post.cont,post.create_time from post join users on post.uid=users.uid where tid="+tid+" order by pid limit "+(nowpage-1)*10+",10;");
				while(rs.next()){
					Map row=new HashMap();
					row.put("uname", rs.getString("uname"));
					row.put("cont", rs.getString("cont"));
					row.put("create_time", rs.getString("create_time"));
					posts.add(row);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				throw e;
			}
		 catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			throw e;
		}
		dao.close();
		return posts;
	}
	
	public static void addTopic(int uid,String theme){
		Dao dao=new Dao();
		try {
			dao.exec("insert into topic values("+uid+",null,'"+theme+"',now())");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void reply(int uid,int tid,String cont){
		Dao dao=new Dao();
		try {
			dao.exec("insert into post values(null,"+uid+","+tid+",'"+cont+"',now())");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static String getSize() throws ClassNotFoundException, SQLException{
		Dao dao=new Dao();
		String size=new String();
		try {
			ResultSet rs=dao.show("select count(*) from topic");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			throw e;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw e;
		}
		dao.close();
		return size;
		
	}
	public static String getPostSize() throws ClassNotFoundException, SQLException{
		Dao dao=new Dao();
		String size=new String();
		ResultSet rs;
		try {
			rs = dao.show("select count(*) from post");
			while(rs.next()){
				size=rs.getString("count(*)");
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			throw e;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw e;
		}
		dao.close();
		return size;
	}
	public static int getMaxTid()throws ClassNotFoundException, SQLException{
		Dao dao=new Dao();
		int tid=0;
		ResultSet rs;
		try {
			rs = dao.show("select Max(tid) from topic");
			while(rs.next()){
				tid=rs.getInt("Max(tid)");
			}
		}catch (SQLException e) {
			// TODO Auto-generated catch block
				throw e;
		}catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			throw e;
		}
		dao.close();
		return tid;
	

	}	
}